package com.jfinal.bbsapp.pub;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import com.jfinal.bbsapp.common.kit.MyAnnotation;
import com.jfinal.bbsapp.pub.PubService;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class PubService {
	private final Logger logger = Logger.getLogger(PubService.class);
	
	@MyAnnotation(memo="获取字典信息")
	public Ret dictlist(String keys) {
		try {
			Record rec = Db.findFirst("select id from sys_dict where dict_key=?", keys);
			List<Record> dlist = new ArrayList<Record>(); 
			if (rec != null) {
				dlist= Db.find("select cast(s.id as char) as id,s.dict_key,s.dict_value,cast(s.parentid as char) as parentid from sys_dict s"
					+ " where s.parentid =?",rec.getLong("id"));
			}
			return Ret.ok("data", dlist).set("code", 20000);
		} catch (Exception e) {
			logger.error("------Exception------", e);
			return Ret.fail("系统异常").set("code", 50000);
		}
	}
	
	@MyAnnotation(memo="获取字典信息")
	public Ret getonedict(String keys) {
		try {
			Record data = Db.findFirst("select * from sys_dict where dict_key=?", keys);
			return Ret.ok("data", data).set("code", 20000);
		} catch (Exception e) {
			logger.error("Exception:" , e);
			return Ret.fail("系统异常请稍后重试").set("code", 50000);
		}
	}
	
	@MyAnnotation(memo="获取关于我们")
	public Ret appinfo() {
		try {
			String appinfo ="";
			Record rec = Db.findFirst("select ifnull(content,'') content from a_agree where agreetype='02'");
			if(rec != null){
				appinfo=rec.getStr("content");
			}
			return Ret.ok("data", appinfo).set("code", 20000);
		} catch (Exception e) {
			logger.error("Exception:" , e);
			return Ret.fail("系统异常请稍后重试").set("code", 50000);
		}
	}
	
	@MyAnnotation(memo="获取注册协议")
	public Ret getagree() {
		try {
			String appinfo ="";
			Record rec = Db.findFirst("select ifnull(content,'') content from a_agree where agreetype='01'");
			if(rec != null){
				appinfo=rec.getStr("content");
			}
			return Ret.ok("data", appinfo).set("code", 20000);
		} catch (Exception e) {
			logger.error("Exception:" , e);
			return Ret.fail("系统异常请稍后重试").set("code", 50000);
		}
	}

}